package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduBanner;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.mapper.EduTeacherMapper;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.service.BannerService;
import com.jf3q.front.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/index")
@Tag(name="前台首页控制层")
public class IndexController {
    @Autowired
    BannerService bannerService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Value("${redis.newCoursesKey}")
    String newCoursesKey;
    @Autowired
    TeacherService teacherService;

    //查询所有banner
    @GetMapping("/banner")
    @Operation(summary = "轮播图展示")
    public ResultVo getAllBanner() {

        LambdaQueryWrapper<EduBanner> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduBanner::getEduCreate);
        //last方法，拼接sql语句
        queryWrapper.last("limit 2");//显示最新的两个图


        List<EduBanner> list = bannerService.list();
        return ResultVo.success(list);
    }

    @Operation(summary = "最新课程")
    @GetMapping("/course")
    public ResultVo getNewCourses(){
       List<EduCourse> newCourses= redisTemplate.opsForList().range(newCoursesKey,0,-1);

       return ResultVo.success(newCourses);
    }

    private String makeOrderByFieldSql(String fieldName, Set<String> fields) {

        StringBuilder orderSql = new StringBuilder();
        orderSql.append("ORDER BY FIELD(").append(fieldName).append(",");
        for (String field : fields) {
            orderSql.append("\"").append(field).append("\"").append(",");
        }
        //去除最后一个 ",",加上括号
        orderSql.deleteCharAt(orderSql.length() - 1).append(")");
        return orderSql.toString();
    }

    @Operation(summary = "名师大咖")
    @GetMapping("/teacher")
    public ResultVo getHotTeacher(){

        //要降序的
        Set zset = redisTemplate.opsForZSet().reverseRange("teacherBuyCount", 0, 3);//查排在前面四个老师


        //2 1 1797897955768750081   1797897696434933761
        if(zset.size()>0){//zset里有数据的时候
            LambdaQueryWrapper<EduTeacher> queryWrapper= new LambdaQueryWrapper<>();

            //保证我的id顺序  ORDER BY
            //	FIELD(
            //		id,
            //		"1797897955768750081",
            //		"1797897696434933761"
            //	)
            String orderSql = this.makeOrderByFieldSql("id", zset);
            // 使用in函数指定要查询的ID列表
            queryWrapper.in(EduTeacher::getId, zset).last(orderSql);

            // 执行查询
            List<EduTeacher> list = teacherService.list(queryWrapper);
            return ResultVo.success(list);
        }else{//zet里没东西的时候（还没一个人买任何老师的课）

            Page<EduTeacher> page = teacherService.page(new Page<>(1, 4));
            // 执行查询
            List<EduTeacher> list =page.getRecords();
            return ResultVo.success(list);

        }
    }
}
